package pl.iachoo.ctl.tabory.ui.editors.vagons;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.Section;

import pl.iachoo.ctl.tabory.dao.api.services.ICompositionService;
import pl.iachoo.ctl.tabory.dao.api.services.IVagonService;
import pl.iachoo.ctl.tabory.dao.data.vo.CompositionVO;
import pl.iachoo.ctl.tabory.dao.data.vo.VagonVO;
import pl.iachoo.ctl.tabory.dao.hibernate.services.ServiceManager;
import pl.iachoo.ctl.tabory.ui.base.EditorStateSL;
import pl.iachoo.ctl.tabory.ui.base.masterdetail.AbstractMasterDetailSection;
import pl.iachoo.ctl.tabory.ui.editors.vagons.composites.VagonDetailSectionPart;
import pl.iachoo.ctl.tabory.ui.editors.vagons.composites.VagonMasterSectionPart;
import pl.iachoo.ctl.tabory.ui.properties.Messages;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;

public class VagonEditor
		extends
		AbstractMasterDetailSection<VagonSelection, VagonMasterSectionPart, VagonDetailSectionPart> {

	private final static Logger log = Logger.getLogger(VagonEditor.class);

	public VagonEditor() throws Exception {
		super();
	}

	public static final String ID = "pl.iachoo.ctl.tabory.ui.editors.vagons.VagonEditor";

	@Override
	protected VagonMasterSectionPart getMasterSection(Form parentForm) {
		Set<VagonMasterSectionPart.AvailableColumnsSL> setAllColumns = new HashSet<VagonMasterSectionPart.AvailableColumnsSL>();
		setAllColumns
				.add(VagonMasterSectionPart.AvailableColumnsSL.COLUMN_COMPOSITION_NAME);
		setAllColumns
				.add(VagonMasterSectionPart.AvailableColumnsSL.COLUMN_VAGON_NAME);
		VagonMasterSectionPart sectionPart = new VagonMasterSectionPart(
				parentForm.getBody(), formToolkit, Section.TITLE_BAR,
				Messages.getString("vagons.editor.table.title"), true, true,
				setAllColumns);
		sectionPart.setSelection(selection);
		return sectionPart;
	}

	@Override
	protected VagonDetailSectionPart getDetailSection(Form parentForm) {
		VagonDetailSectionPart sectionPart = new VagonDetailSectionPart(
				parentForm.getBody(), formToolkit, Section.TITLE_BAR,
				Messages.getString("vagons.editor.details"));
		sectionPart.setSelection(selection);
		return sectionPart;
	}

	@Override
	protected void postInitGui(Composite parent) {
		masterSectionPart
				.addSelectionListener(new ISelectionListener<VagonSelection>() {
					@Override
					public void selectionChanged(VagonSelection selection) {
						log.debug("Selection changed.");
						EditorStateSL editorStateSL = selection
								.getEditorState();
						if (editorStateSL.equals(EditorStateSL.CLEAN)) {
							setDirty(false);
						} else {
							setDirty(true);
						}
						masterSectionPart.setSelection(selection);
						detailSectionPart.setSelection(selection);
					}
				});
	}

	@Override
	protected void doSave() throws Exception {
		IVagonService vagonService = ServiceManager.getVagonService();
		VagonSelection saveSelection = detailSectionPart.getSelection();
		VagonVO selectedVagon = saveSelection.getSelectedVagon();
		vagonService.saveUpdateVagon(selectedVagon);
		selection = initSelection();
		masterSectionPart.setSelection(selection);
		detailSectionPart.setSelection(selection);
		setDirty(false);
	}

	@Override
	protected void doCancel() throws Exception {
		selection = initSelection();
		masterSectionPart.setSelection(selection);
		detailSectionPart.setSelection(selection);
		setDirty(false);
	}

	@Override
	public void setFocus() {
		setDirty(true);
		editorCancelAction.run();
	}

	@Override
	protected VagonSelection initSelection() throws Exception {
		log.debug("Creating VagonSelection.");
		IVagonService vagonService = ServiceManager.getVagonService();
		ICompositionService compositionService = ServiceManager
				.getCompositionService();
		VagonSelection selection = new VagonSelection();
		List<VagonVO> allVagons = vagonService.getListAllVagons();
		List<CompositionVO> allCompositions = compositionService
				.getListAllCompositions();
		selection.setListVagons(allVagons);
		selection.setListCompositions(allCompositions);
		selection.setEditorState(EditorStateSL.CLEAN);
		log.debug("VagonSelection created: " + selection);
		return selection;
	}

}
